import matplotlib.pyplot as plt
import numpy as np
from test0 import create_coordinate_system

def sgn(x):
    return np.where(x > 0, 1, np.where(x < 0, -1, 0))


# 创建一个坐标系
fig, ax = create_coordinate_system(x_range=(-3, 3), y_range=(-3, 3), figsize=(8, 6))
# 将x范围分为三部分，避免在0点处连接
x_left = np.linspace(-3, -0.01, 50)  # 负半轴，不包含0
x_right = np.linspace(0.01, 3, 50)   # 正半轴，不包含0
x_zero = np.array([0])  

# 计算对应的y值
y_left = sgn(x_left)
y_right = sgn(x_right)
y_zero = sgn(x_zero)

# 分别绘制三个部分
ax.plot(x_left, y_left, 'r-', linewidth=2, label='sgn(x)')
ax.plot(x_right, y_right, 'r-', linewidth=2)
ax.plot(x_zero, y_zero, 'ro', markersize=5)  # 在原点处绘制一个点

ax.legend()
plt.show()
